package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.GoodClassAdapter;
import com.xgkj.diyiketang.activity.data.SubjectAdapter;
import com.xgkj.diyiketang.data.VideoInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.MoreVideoListTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.util.PopupHelper;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

public class GoodClassActivity extends BaseActivity implements View.OnClickListener,PullToRefreshListView.OnLoadDataListener,PullToRefreshListView.OnRefreshListener{
    private Context mContext;
    private RelativeLayout titleLayout;
    private TextView back;
    private TextView className;
    private LinearLayout xuanzeLayout;
    private RelativeLayout chooseKemu;
    private TextView kemu;
    private RelativeLayout chooseNianji;
    private TextView nianji;
    private RelativeLayout choosePaixu;
    private TextView paixu;
    private ImageView fegexian;
    private PullToRefreshListView goodclassList;
    private GoodClassAdapter mAdapter;
    private int pageNo;
    private int size = 20;
    private String mType;
    private String mSubjectNo;
    private String mGrade;
    private String orderNo;
    private int listtaskId = -1;
    private NoDataUtil noDataUtil;
    private PopupHelper mSubjectPopWindow;
    private PopupHelper mGradePopWindow;
    private PopupHelper mOrderPopWindow;
    private SubjectAdapter mSubjectAdapter;
    private SubjectAdapter mGradeAdapter;
    private SubjectAdapter mOrderAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fine_course);
        mContext = this;
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        if (intent != null){
            mSubjectNo = intent.getStringExtra("SUBJECT");
        }
        titleLayout = (RelativeLayout) findViewById(R.id.title_layout);
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(this);
        className = (TextView) findViewById(R.id.className);
        xuanzeLayout = (LinearLayout) findViewById(R.id.xuanze_layout);
        chooseKemu = (RelativeLayout) findViewById(R.id.choose_kemu);
        chooseKemu.setOnClickListener(this);
        kemu = (TextView) findViewById(R.id.kemu);
        chooseNianji = (RelativeLayout) findViewById(R.id.choose_nianji);
        chooseNianji.setOnClickListener(this);
        nianji = (TextView) findViewById(R.id.nianji);
        choosePaixu = (RelativeLayout) findViewById(R.id.choose_paixu);
        choosePaixu.setOnClickListener(this);
        paixu = (TextView) findViewById(R.id.paixu);
        fegexian = (ImageView) findViewById(R.id.fegexian);
        goodclassList = (PullToRefreshListView) findViewById(R.id.goodclass_list);
        mAdapter = new GoodClassAdapter(mContext);
        goodclassList.init(mContext,true);
        goodclassList.setAdapter(mAdapter,true);
        goodclassList.setOnLoadDataListener(this);
        goodclassList.setOnRefreshListener(this);
        View view = findViewById(R.id.content_layout);
        noDataUtil = new NoDataUtil(mContext,view);
        goodclassList.doRefresh();
        initPopWindow();
    }

    private void initPopWindow() {
        mSubjectAdapter = new SubjectAdapter(mContext,1);
        int currentSubject = -1;
        try {
            currentSubject = Integer.parseInt(mSubjectNo);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        mSubjectAdapter.setSelectPosition(currentSubject+1);
        kemu.setText((String) mSubjectAdapter.getItem(currentSubject+1));
        mSubjectPopWindow = new PopupHelper(mContext,mSubjectAdapter);
        mSubjectPopWindow.setOnDisMissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                kemu.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.smallxiajiantou,0);
            }
        });
        mSubjectPopWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mSubjectPopWindow.dismissDropDownView();
                if (position == 0){
                    mSubjectNo = null;
                }else {
                    mSubjectNo = String.valueOf(position-1);
                }
                kemu.setText((String) mSubjectAdapter.getItem(position));
                mSubjectAdapter.setSelectPosition(position);
                goodclassList.doRefresh();
            }
        });

        mGradeAdapter = new SubjectAdapter(mContext,2);
        mGradePopWindow = new PopupHelper(mContext,mGradeAdapter);
        mGradePopWindow.setOnDisMissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                nianji.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.smallxiajiantou,0);
            }
        });
        mGradePopWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mGradePopWindow.dismissDropDownView();
                if (position == 0){
                    mGrade = null;
                }else {
                    mGrade = String.valueOf(position);
                }
                nianji.setText((String)mGradeAdapter.getItem(position));
                mGradeAdapter.setSelectPosition(position);
                goodclassList.doRefresh();
            }
        });

        mOrderAdapter = new SubjectAdapter(mContext,3);
        mOrderPopWindow = new PopupHelper(mContext,mOrderAdapter);
        mOrderPopWindow.setOnDisMissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                paixu.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.smallxiajiantou,0);
            }
        });
        mOrderPopWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mOrderPopWindow.dismissDropDownView();
                if (position == 0){
                    orderNo = null;
                }else {
                    orderNo = String.valueOf(position-1);
                }
//                orderNo = String.valueOf(position);
                paixu.setText((String) mOrderAdapter.getItem(position));
                mOrderAdapter.setSelectPosition(position);
                goodclassList.doRefresh();
            }
        });
    }


    private void getGoodClassList(){
        if (pageNo ==0){
            goodclassList.setNoNextPage();
            return;
        }

        MoreVideoListTask task = new MoreVideoListTask(size,pageNo,mType,mSubjectNo,mGrade,orderNo);
        task.addListener(mListener);
        listtaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private TaskEventListener mListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == listtaskId){
                goodclassList.onRefreshComplete();
                if (pageNo == 1){
                    mAdapter.clearData();
                    goodclassList.enableLoadMore();
                }else {
                    goodclassList.onLoadDataComplete(true);
                }
                List<VideoInfo> attentionList = new ArrayList<VideoInfo>();
                if(data != null){
                    attentionList = (List<VideoInfo>) data;
                }

                if(attentionList != null && attentionList.size()>0){
                    mAdapter.updateData(attentionList);
                }

                pageNo = result;
                if (mAdapter != null && mAdapter.getCount()== 0){
                    noDataUtil.ListViewEmpty(goodclassList,null,null,-1);
                }else {
                    noDataUtil.ListViewNoEmpty(goodclassList);
                }
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (pageNo == 1) {
                goodclassList.onRefreshComplete();
            }else {
                goodclassList.onLoadDataComplete(false);
            }

            if (mAdapter != null && mAdapter.getCount()==0){
                noDataUtil.ListViewError(goodclassList, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getGoodClassList();
                    }
                });
            }

        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.choose_kemu:
                kemu.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.smallshangjiantou,0);
                mSubjectPopWindow.showDropDownView(v);
                break;
            case R.id.choose_nianji:
                nianji.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.smallshangjiantou,0);
                mGradePopWindow.showDropDownView(v);
                break;
            case R.id.choose_paixu:
                paixu.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.smallshangjiantou,0);
                mOrderPopWindow.showDropDownView(v);
                break;
        }
    }

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getGoodClassList();
    }

    @Override
    public void onRefresh() {
        pageNo = 1;
        noDataUtil.ListViewNoEmpty(goodclassList);
        getGoodClassList();
    }
}
